import 'package:flutter/material.dart';
import 'package:flutter_app/global/color_config.dart';
import 'package:flutter_app/login/login.dart';
import 'package:flutter_app/main/content/content.dart';
import 'package:flutter_app/main/basemain.dart';
import 'package:flutter_app/main/main/system/system_details.dart';
import 'package:flutter_app/util/sp/login_key.dart';
import 'package:flutter_app/util/sp_util.dart';

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: "/",
      routes: {
        ContentWidget.RouteName: (context) => const ContentWidget(),
        SystemDetails.RouteName: (context) => const SystemDetails(),
      },
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: ColorConfig.materialColor,
      ),
      home: const SplashPage(title: 'Flutter Demo Home Page'),
    );
  }
}

class SplashPage extends StatefulWidget {
  const SplashPage({super.key, required this.title});

  final String title;

  @override
  SplashPageState createState() => SplashPageState();
}

class SplashPageState extends State<SplashPage>
    with SingleTickerProviderStateMixin {
  late AnimationController controller;
  late Animation<double> animation;

  @override
  void initState() {
    controller =
        AnimationController(duration: const Duration(seconds: 2), vsync: this);
    animation = Tween(begin: .1, end: 1.0).animate(controller)
      ..addStatusListener((status) {
        if (status == AnimationStatus.completed) {
          /**
           * 动画执行完毕，延迟500毫秒跳转到首页
           */
          Future.delayed(const Duration(milliseconds: 500), () async {
            if (await SPUtil.getBool(LoginKey.loginStateKey, false)) {
              Navigator.pushAndRemoveUntil(
                context,
                MaterialPageRoute(builder: (context) => const MainWidget()),
                    (route) => route == null,
              );
            } else {
              Navigator.pushAndRemoveUntil(
                context,
                MaterialPageRoute(builder: (context) => const LoginWidget()),
                    (route) => route == null,
              );
            }
          });
        }
      });
    controller.forward();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      color: Colors.white,
      child: Container(
        decoration: const BoxDecoration(color: Color(0xFF4184F2)),
        child: ScaleTransition(
          scale: animation,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: const <Widget>[
              Image(
                image: AssetImage("images/ic_splash.png"),
                width: 70,
              ),
              Text(
                "  Play Android",
                textAlign: TextAlign.start,
                style: TextStyle(
                    color: Colors.white,
                    inherit: false,
                    fontSize: 36,
                    height: 2),
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    controller!.dispose();
    super.dispose();
  }
}
